/*
 * @Author: your name
 * @Date: 2021-05-12 13:55:02
 * @LastEditTime: 2021-05-12 15:10:25
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \koa2-serve\middlewares\filter.js
 */
const tracer = require("tracer");
const logger = tracer.colorConsole({
    level: "error",
    format: "{{timestamp}} <{{title}}> {{file}}(#{{line}}): {{message}}",
    file: "error.log",
    path: __dirname,
});

module.exports = async (ctx, next) => {
    try {
        await next();
    } catch (err) {
        if (!err) {
            return ctx.error({ error: new Error("未知错误!") });
        }
        if (typeof err == "string") {
            return ctx.error({ error: new Error(err) });
        }
        logger.error(err.stack);
        ctx.error({ msg: "服务器错误!", error: err.stack, status:500});
    }
};
